#include "mainwindow.h"
#include "mainwindow_filling.h"

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent), ui(new Ui::MainWindow)
{
	ui->setupUi(this);
//	if ( conf.contains("WMstate") ) {
		this->restoreGeometry(conf.value("WMstate").toByteArray());
//	}

	if (!checkDb()) {
		qWarning() << "Can't live without database";
		qApp->exit(1);
	}
	this->tracTable = ui->tracTable;
	this->getAllFromDb();
	connect(ui->adjustButton, SIGNAL(clicked()), tracTable, SLOT(resizeColumnsToContents()));
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	conf.setValue("WMstate", this->saveGeometry());
	QWidget::closeEvent(event);
}

MainWindow::~MainWindow() {
	delete ui;
}

void MainWindow::getAllFromDb() {
	model = new MyModel(tracTable, dbh);
//	model->setTable("tasks");
//	model->setRelation(6, QSqlRelation("priorities", "p_id", "p_name"));
//	model->setRelation(7, QSqlRelation("statuses", "s_id", "s_name"));
//	model->setRelation(1, QSqlRelation("tasks", "owner", "owner"));
//	model->select();
	tracTable->setModel(model);
	MyDelegate *test = new MyDelegate(tracTable);
	tracTable->setItemDelegate(new MyDelegate(tracTable));
	tracTable->setSortingEnabled(true);
	tracTable->setSelectionBehavior(QAbstractItemView::SelectRows);
	tracTable->sortByColumn(0, Qt::AscendingOrder);
	tracTable->resizeColumnsToContents();
	tracTable->horizontalHeader()->setStretchLastSection(true);
	tracTable->horizontalHeader()->setMovable(true);
	tracTable->horizontalHeader()->setCascadingSectionResizes(true);

	connect(model, SIGNAL(sortColumnChanged(int)), tracTable, SLOT(resizeColumnToContents(int)));
	connect(model, SIGNAL(message(const QString&)), this, SLOT(setStatusMessage(const QString&)));
	connect(model, SIGNAL(message(QStringList&)), this, SLOT(setStatusMessage(QStringList&)));
//	connect(ui->addButton, SIGNAL(clicked()), this, SLOT(insertRow()));
}

void MainWindow::insertRow() {
	ui->statusBar->showMessage("inserting row");
	model->addRow();
}

void MainWindow::deleteRow() {
	int submit = QMessageBox::question(this, tr("really delete?"), tr("Do you really want to delete task(s)?"), QMessageBox::Ok, QMessageBox::Cancel);
	if ( submit == QMessageBox::Ok ) {
		model->deleteRow(tracTable->selectionModel()->selectedRows());
		tracTable->clearSelection();
	}
}

void MainWindow::startTask() {
	QModelIndexList srL = tracTable->selectionModel()->selectedRows();
	int count = srL.count();
	if ( count == 0 ) {
		setStatusMessage(tr("No tasks selected"));
		return;
	}
	int submit = QMessageBox::Ok;
	if ( count > 1 )
		submit = QMessageBox::question(this, tr("Really start %n task(s)?", "", count),
					tr("Do you really want to start %n task(s)?", "", count), QMessageBox::Ok, QMessageBox::Cancel);
	if ( submit == QMessageBox::Ok ) {
		model->setStartedRows(srL);
//		tracTable->clearSelection();
	}
}

void MainWindow::viewSummary() {
	SummaryForm f(model, this);
	f.exec();
}

void MainWindow::setStatusMessage(QStringList &message) {
	qDebug() << message;
	QString mess = message.first();
	message.pop_front();
	if (message.count() > 0)
		mess = mess+": "+message.join(", ");
	ui->statusBar->showMessage(mess);
}

void MainWindow::setStatusMessage(const QString &mess) {
	ui->statusBar->showMessage(mess);
}



bool MainWindow::checkDb() {
	dbh = QSqlDatabase::addDatabase("QSQLITE");
	dbh.setDatabaseName(dbFile());
	if (!QFileInfo(dbFile()).exists()) {
		if ( !dbh.open() ) {
			qWarning() << "Can't create database file";
			return false;
		}
		if ( !this->fillDb() ) {
			qWarning() << "Can't fill db!";
			return false;
		}
	}
	if ( !dbh.open() ) {
		qWarning() << "Can't open database file";
		return false;
	}
	return true;
}




